package com.example.xd.commen_utils.installpackageinfo

import android.content.Context
import android.database.Cursor
import android.provider.CallLog
import android.provider.ContactsContract
import android.util.Log
import com.ebupt.defaultcallphone.defaultcall.demo.CallLogBean
import com.ebupt.defaultcallphone.defaultcall.demo.ContactsBean
import java.text.SimpleDateFormat
import java.util.*

/**
 * @auther XuDan on 2020/9/3.
 */
object ContactsUtil {
    /**
     * 获取通讯录好友
     * <uses-permission android:name="android.permission.READ_CONTACTS" />
     */
    fun getAddressList(context: Context): MutableList<ContactsBean> {
        val list = mutableListOf<ContactsBean>()
        var cursor: Cursor? = null
        var contactsBean: ContactsBean? = null
        try {
            cursor = context.contentResolver.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,
                null,
                null,
                null
            )
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    var displayName =
                        cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
                    var number =
                        cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                    contactsBean = ContactsBean(displayName, number)
                    list.add(contactsBean)
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            if (cursor != null) {
                cursor.close()
            }
        }
        return list
    }

    /**
     * 获取联系人
     * <uses-permission android:name="android.permission.READ_CALL_LOG" />
     * <uses-permission android:name="android.permission.READ_CONTACTS" />
     */
    fun getCallLogList(context: Context): MutableList<CallLogBean> {
        val list = mutableListOf<CallLogBean>()
        var contactsBean: CallLogBean
        var cursor = context.getContentResolver().query(
            CallLog.Calls.CONTENT_URI,
            arrayOf(
                CallLog.Calls.CACHED_NAME,  //姓名
                CallLog.Calls.NUMBER,  //号码
                CallLog.Calls.DATE,  //呼入/呼出(2)/未接
                CallLog.Calls.DURATION,  //拨打时间
                CallLog.Calls.TYPE
            ), null,
            null,
            CallLog.Calls.DEFAULT_SORT_ORDER
        )
        if (cursor != null && cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                var callName: String? =
                    cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME))
                val callNumber: String =
                    cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)) //号码
                val data: Long = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE))
                var call_data: String = formateData(data)
                val duration = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.DURATION))
                var call_duration = formateDuration(duration)
                val type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE))
                var call_type = "OUTGOING_TYPE"
                when (type) {
                    CallLog.Calls.INCOMING_TYPE -> {
                        call_type = "INCOMING_TYPE"//打入
                    }
                    CallLog.Calls.OUTGOING_TYPE -> {
                        call_type = "OUTGOING_TYPE"//打出
                    }
                    CallLog.Calls.MISSED_TYPE -> {
                        call_type = "MISSED_TYPE"//未接
                    }
                }
                contactsBean =
                    CallLogBean(callName, callNumber, call_data, call_type, call_duration)
                list.add(contactsBean)
                cursor.moveToNext()
            }
        }
        return list
    }

    private fun formateData(data: Long): String {
        //拨打时间
        val date = Date(System.currentTimeMillis())
        val simpleDateFormat = SimpleDateFormat("yyyy年MM月dd日")
        val date_today = simpleDateFormat.format(date)

        var sdf = SimpleDateFormat("yyyy年MM月dd日")
        val callDate = Date(data)
        var callDateStr: String = sdf.format(callDate)

        if (callDateStr == date_today) { //判断是否为今天
            sdf = SimpleDateFormat("HH:mm")
            callDateStr = sdf.format(callDate)
        } else if (date_today.contains(callDateStr.substring(0, 7))) { //判断是否为当月
            sdf = SimpleDateFormat("dd")
            val callDay: Int = Integer.valueOf(sdf.format(callDate))
            val day: Int = Integer.valueOf(sdf.format(date))
            if (day - callDay == 1) {
                callDateStr = "昨天"
            } else {
                sdf = SimpleDateFormat("MM月dd日")
                callDateStr = sdf.format(callDate)
            }
        } else if (date_today.contains(callDateStr.substring(0, 4))) { //判断是否为当年
            sdf = SimpleDateFormat("MM月dd日")
            callDateStr = sdf.format(callDate)
        }
        return callDateStr
    }

    private fun formateDuration(duration: Int): String {
        val min: Int = duration / 60
        val sec: Int = duration % 60
        var callDurationStr = ""
        if (sec > 0) {
            callDurationStr = if (min > 0) {
                min.toString() + "分" + sec + "秒"
            } else {
                sec.toString() + "秒"
            }
        }
        return callDurationStr
    }
}